package z.explorer.ui.component.walker;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

import z.explorer.Main;
import z.explorer.core.storage.Storage;
import z.explorer.core.storage.dao.StorageDAO;
import z.explorer.ui.common.Handler;
import org.jfree.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

public class Distribution extends JPanel {

	DefaultPieDataset pieDataset = new DefaultPieDataset();
	DefaultCategoryDataset ds = new DefaultCategoryDataset();
	
	String values[] = {"Line Chart","Bar Chart"};
	JComboBox ChartList = new JComboBox(values);
	
	JFreeChart lineChart = null;
	JFreeChart barChart = null;
	
	ChartPanel topChartPanel = null;
	ChartPanel bottomChartPanel = null;
	JPanel bottomPanel = null;
	public Distribution() {
		initData();
		initUI();
	}

	public void initData() {
		StorageDAO sdao = (StorageDAO) Main.context.getBean("storageDAO");
		List<Storage> list = sdao.getDistributionByDay(new Date());
		for (int i = 0; i < list.size(); i++) {
			pieDataset.setValue(list.get(i).getOwner(), list.get(i).getGb());
			List<Storage> l = sdao.getDistributionByOwner(list.get(i)
					.getOwner());

			for (int j = 0; j < l.size(); j++) {
				ds.addValue(l.get(j).getGb(), l.get(j).getOwner(), l.get(j)
						.getYyyymmdd());
			}
		}

	}

	public void initTop() {
		JFreeChart pieChart = ChartFactory.createPieChart("Pie Chart "
				+ new Date(), // chart title
				pieDataset, // data
				false, // include legend
				true, false);
		PiePlot plot = (PiePlot) pieChart.getPlot();
		plot.setSectionOutlinesVisible(false);
		plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 8));
		plot.setNoDataMessage("No data available");
		plot.setSimpleLabels(true);
		topChartPanel = new ChartPanel(pieChart);

	}

	public void initBottom() {
		
		ChartList.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String name=(String)ChartList.getSelectedItem();
				changeChart(name);
			}
			
		});
		
		
		JPanel listPanel = new JPanel();
		listPanel.setLayout(new FlowLayout());
		JLabel label = new JLabel("Chart:");
		listPanel.add(label);
		listPanel.add(ChartList);
		
		
		lineChart = ChartFactory.createLineChart("increase", "day",
				"GB", ds, PlotOrientation.VERTICAL, true, true, false);
//		lineChart.setBackgroundPaint(Color.LIGHT_GRAY);
		CategoryPlot plot = (CategoryPlot) lineChart.getPlot();
		plot.setBackgroundPaint(Color.WHITE);
		plot.setRangeGridlinePaint(Color.BLUE);
		plot.setNoDataMessage("No data avialable");
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot
				.getRenderer();
		renderer.setShapesVisible(true);
		renderer.setDrawOutlines(true);
		renderer.setUseFillPaint(true);
		renderer.setFillPaint(Color.WHITE);
		
		barChart = ChartFactory.createBarChart("increase", "day", "GB",
				ds, PlotOrientation.VERTICAL, true, true, false);
//		barChart.setBackgroundPaint(Color.LIGHT_GRAY);
		CategoryPlot bplot = (CategoryPlot) lineChart.getPlot();
		bplot.setBackgroundPaint(Color.WHITE);
		bplot.setRangeGridlinePaint(Color.BLUE);
		bplot.setNoDataMessage("No data avialable");
		NumberAxis brangeAxis = (NumberAxis) bplot.getRangeAxis();
		brangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		LineAndShapeRenderer brenderer = (LineAndShapeRenderer) bplot
				.getRenderer();
		brenderer.setShapesVisible(true);
		brenderer.setDrawOutlines(true);
		brenderer.setUseFillPaint(true);
		brenderer.setFillPaint(Color.WHITE);
				
		bottomChartPanel = new ChartPanel(lineChart);
		
		bottomPanel = new JPanel();
		bottomPanel.setLayout(new BorderLayout());
		bottomPanel.add(listPanel,BorderLayout.NORTH);
		bottomPanel.add(bottomChartPanel,BorderLayout.CENTER);
		

	}
	
	public void changeChart(String name){
		if(name.equals("Line Chart")){
			bottomChartPanel.setChart(lineChart);
		}else if(name.equals("Bar Chart")){
			bottomChartPanel.setChart(barChart);
		}
		
	}
	
	public void initUI() {
		initTop();
		initBottom();
		setLayout(new BorderLayout());
		JSplitPane separator = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true);
		separator.setTopComponent(topChartPanel);
		separator.setBottomComponent(bottomPanel);
		separator.setDividerLocation(0.6);
		separator.setOneTouchExpandable(true);
		separator.setResizeWeight(0.2);
		add(separator, BorderLayout.CENTER);
	}

}
